package com.kla.power.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.kla.power.entity.InverterElectricEntity;
import com.kla.power.entity.LatestInverterElectricEntity;
import com.kla.power.entity.StationEntity;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 逆变器发电数据 Mapper 接口
 * </p>
 * @author WuZH
 * @since 2024-06-02
 */
@Mapper
public interface LatestInverterElectricMapper extends BaseMapper<LatestInverterElectricEntity> {

    @Insert (
            "INSERT INTO latest_inverter_electric_info (" +
                    "inverTemp, gridPower, totaEnergy, totalHours, operModel, " +
                    "todayEnergy, channelAEnergy, channelBEnergy, sinkTemp, busVoltage, pwrOnCnt, " +
                    "serialNumber, phaseNumber, vaRating, firmwareVer, madelName, manufacturer, " +
                    "nomVpv, interVersion, collectorSn, stationId" +
                    ") VALUES (" +
                    "#{inverTemp}, #{gridPower}, #{totaEnergy}, #{totalHours}, #{operModel}, " +
                    "#{todayEnergy}, #{channelAEnergy}, #{channelBEnergy}, #{sinkTemp}, #{busVoltage}, #{pwrOnCnt}, " +
                    "#{serialNumber}, #{phaseNumber}, #{vaRating}, #{firmwareVer}, #{madelName}, #{manufacturer}, " +
                    "#{nomVpv}, #{interVersion}, #{collectorSn}, #{stationId}" +
                    ") " +
                    "ON DUPLICATE KEY UPDATE " +
                    "inverTemp = VALUES(inverTemp), " +
                    "gridPower = VALUES(gridPower), " +
                    "totaEnergy = VALUES(totaEnergy), " +
                    "totalHours = VALUES(totalHours), " +
                    "operModel = VALUES(operModel), " +
                    "todayEnergy = VALUES(todayEnergy), " +
                    "channelAEnergy = VALUES(channelAEnergy), " +
                    "channelBEnergy = VALUES(channelBEnergy), " +
                    "sinkTemp = VALUES(sinkTemp), " +
                    "busVoltage = VALUES(busVoltage), " +
                    "pwrOnCnt = VALUES(pwrOnCnt)"
    )
    void saveOrUpdateSingle (LatestInverterElectricEntity entity);


    /**
     * 一个商户下所有电站下的所有逆变器此刻发电列表
     * @param stationEntity
     * @return
     */
    @Select (value = "select si.id,\n" +
            "       liei.totaEnergy,\n" +
            "       liei.todayEnergy,\n" +
            "       si.stationName,\n" +
            "       liei.gridPower,\n" +
            "       di.deviceSn,\n" +
            "       di.power,\n" +
            "       si.address,\n" +
            "       liei.updateTime\n" +
            "from latest_inverter_electric_info liei\n" +
            "         LEFT JOIN device_info di ON liei.serialNumber = di.deviceSn\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "WHERE si.merchantId IN (SELECT id\n" +
            "                        FROM (SELECT t1.id,\n" +
            "                                     t1.parentId,\n" +
            "                                     IF(find_in_set(parentId, @pids) > 0, @pids := concat(@pids, ',', id), 0) AS ischild\n" +
            "                              FROM (SELECT id, parentId FROM merchant_info t) t1,\n" +
            "                                   (SELECT @pids := #{stationEntity.merchantId}) t2) t3\n" +
            "                        where ischild != 0\n" +
            "                        UNION\n" +
            "                        SELECT id\n" +
            "                        FROM merchant_info pi\n" +
            "                        WHERE pi.id = #{stationEntity.merchantId})")
    List<StationEntity> queryPowerGroupStation (@Param ("stationEntity") StationEntity stationEntity);


    @Select ( value = "\n" +
            "SELECT\n" +
            "       SUM(liei.todayEnergy)            AS todayEnergy,\n" +
            "       si.stationName,\n" +
            "       COALESCE(SUM(liei.gridPower), 0) AS gridPower,\n" +
            "       di.deviceSn,\n" +
            "       COALESCE(SUM(di.power), 0)      AS power,\n" +
            "       si.address,\n" +
            "       liei.updateTime\n" +
            "from station_info si\n" +
            "         LEFT JOIN device_info di ON si.id = di.stationId\n" +
            "         LEFT JOIN latest_inverter_electric_info liei ON di.deviceSn=liei.serialNumber\n" +
            "WHERE si.merchantId IN (SELECT id\n" +
            "                        FROM (SELECT t1.id,\n" +
            "                                     t1.parentId,\n" +
            "                                     IF(find_in_set(parentId, @pids) > 0, @pids := concat(@pids, ',', id), 0) AS ischild\n" +
            "                              FROM (SELECT id, parentId FROM merchant_info t) t1,\n" +
            "                                   (SELECT @pids := #{stationEntity.merchantId}) t2) t3\n" +
            "                        where ischild != 0\n" +
            "                        UNION\n" +
            "                        SELECT id\n" +
            "                        FROM merchant_info pi\n" +
            "                        WHERE pi.id = #{stationEntity.merchantId})\n" +
            "  AND si.type = 1\n" +
            "GROUP BY si.id\n")
    List<StationEntity> queryFullHour (@Param ("stationEntity") StationEntity stationEntity);

    @Select ( value = "SELECT liei.todayEnergy,\n" +
            "       liei.totaEnergy,\n" +
            "       si.stationName,\n" +
            "       di.deviceSn,\n" +
            "       di.deviceTitle AS inverterTitle,\n" +
            "       liei.gridPower,\n" +
            "       si.capacity,\n" +
            "       si.address,\n" +
            "       liei.updateTime\n" +
            "FROM device_info di\n" +
            "         LEFT JOIN station_info si ON di.stationId = si.id\n" +
            "         LEFT JOIN latest_inverter_electric_info liei ON liei.serialNumber = di.deviceSn\n" +
            "WHERE si.id = #{stationEntity.id}\n" +
            "  AND di.deviceType = 2\n" +
            "GROUP BY liei.serialNumber")
    List<InverterElectricEntity> queryPowerGroupInverter (@Param ("stationEntity") StationEntity stationEntity);

}
